package com.ymdx.action;

import com.opensymphony.xwork2.ModelDriven;
import com.ymdx.entity.News;
import com.ymdx.service.NewsService;
import com.ymdx.util.Page;
import com.ymdx.util.tool.BeanCopyUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 新闻控制层
 * Created by yangliwei on 2017/8/14.
 */
public class NewsAction extends GenericActionSupport implements ModelDriven<News> {
    private News news;

    @Resource
    private NewsService newsService;
    private Page<News> page;
    private Integer pageSize=9;
    private Integer pageNo=1;


    /**
     * 新闻分页列表
     *
     * @return
     */
    @Action(value = "/news/page", results = @Result(name = SUCCESS, type = JSON, params = {
            "root", "page",
            "includeProperties", "pageNo,pageSize,totalCount,result\\[\\d+\\]\\.id,result\\[\\d+\\]\\.title,result\\[\\d+\\]\\.introduction,result\\[\\d+\\]\\.author,result\\[\\d+\\]\\.label,result\\[\\d+\\]\\.publishTime"
    }))
    public String page() {
        String title = news.getTitle();
        String lable = news.getLabel();
        String author = news.getAuthor();
        Map<String,Object> args = new HashMap<>();
        if(StringUtils.isNotBlank(title)){
            args.put("title",title);
        }
        if(StringUtils.isNotBlank(lable)){
            args.put("lable",lable);
        }
        if(StringUtils.isNotBlank(author)){
            args.put("author",author);
        }
        if(page == null){
            page = new Page<News>();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
        }
        page = newsService.page(page,args);
        responseTool.setStatus(1);
        responseTool.setData(page);
        System.out.println(page.getResult().size());
        return SUCCESS;
    }

    /**
     * 查看新闻内容
     *
     * @return
     */
    @Action(value = "/news/detail", results = @Result(name = SUCCESS, type = JSON, params = {
            "root", "responseTool",
            "includeProperties", "status,code,message,data\\.id,data\\.title,data\\.introduction,data\\.author,data\\.label,data\\.content,data\\.publishTime"
    }))
    public String detail() {
        Long id = news.getId();
        if (id == null) {
            responseTool.setStatus(0);
            responseTool.setCode("parameter_empty");
            return SUCCESS;
        }
        News oldNews = newsService.getById(News.class, id);
        if (oldNews == null) {
            responseTool.setStatus(0);
            responseTool.setCode("new_not_exist");
            return SUCCESS;
        }
        responseTool.setStatus(1);
        responseTool.setData(oldNews);
        return SUCCESS;
    }

    /**
     * 保存新闻
     * 这里需要权限控制
     *
     * @return
     */
    @Action(value = "/admin/news/save", results = {@Result(name = SUCCESS, type = JSON, params = {
            "root", "responseTool",
            "includeProperties", "status,code,message,data\\.id"
    })})
    public String save() {
        Object admin = session.get("admin");
        if (admin == null) {
            responseTool.setStatus(0);
            responseTool.setCode("not_enough_power");
            return SUCCESS;
        }
        Long id = news.getId();
        String title = news.getTitle();
        String introduction = news.getIntroduction();
        String lable = news.getLabel();
        String author = news.getAuthor();
        String content = news.getContent();
        if (StringUtils.isAnyBlank(title, introduction, lable, author, content)) {
            responseTool.setStatus(0);
            responseTool.setCode("parameter_empty");
            return SUCCESS;
        }
        News saveNews = null;
        if (id != null) {
            saveNews = newsService.getById(News.class, id);
            if (saveNews == null) {
                responseTool.setStatus(0);
                responseTool.setCode("new_not_exist");
                return SUCCESS;
            }
        }
        //新建的新闻需要手动输入 createTime 和publishTime
        if (saveNews == null) {
            saveNews = new News();
            saveNews.setCreateTime(new Date());
            saveNews.setPublishTime(new Date());
        }
        BeanCopyUtil.copyProperties(news, saveNews, true);
        newsService.save(saveNews);
        responseTool.setStatus(1);
        responseTool.setData(saveNews);

        return SUCCESS;
    }

    @Action(value = "/admin/news/delete", results = {@Result(name = SUCCESS, type = JSON, params = {
            "root", "responseTool",
            "includeProperties", "status,code,message,data\\.id"
    })})
    public String delete() {
        Long id = news.getId();
        if (id == null) {
            responseTool.setStatus(0);
            responseTool.setCode("parameter_empty");
            return SUCCESS;
        }
        News oldNews = newsService.getById(News.class, id);
        if (oldNews == null) {
            responseTool.setStatus(0);
            responseTool.setCode("new_not_exist");
            return SUCCESS;
        }

        newsService.delete(oldNews);
        responseTool.setStatus(1);
        responseTool.setCode("delete_success");
        return SUCCESS;
    }

    @Override
    public News getModel() {
        if (news == null) {
            news = new News();
        }
        return news;
    }

    public Page<News> getPage() {
        return page;
    }

    public void setPage(Page<News> page) {
        this.page = page;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }
}
